package cn.explink.dop.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.list.SetUniqueList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.explink.dop.dao.RestaurantDao;
import cn.explink.dop.dao.StorefrontDao;
import cn.explink.dop.entity.vo.StorefrontVO;
import cn.explink.dop.service.StorefrontService;
import cn.explink.eap.core.common.service.impl.CommonServiceImpl;

/**
 * 业务层
 *
 * @author firefly 2015-2-27 10:36
 *
 */

@Service("storefrontService")
public class StorefrontServiceImpl extends CommonServiceImpl implements StorefrontService {

	@Autowired
	private StorefrontDao storefrontDao;
	@Autowired
	private RestaurantDao restaurantDao;

	@Override
	public String add(final StorefrontVO sto) {
		return (String) this.storefrontDao.save(sto);
	}

	@Override
	public void modify(final StorefrontVO sto) {
		this.storefrontDao.modify(sto);
	}

	public StorefrontDao getStorefrontDao() {
		return this.storefrontDao;
	}

	public void setStorefrontDao(StorefrontDao storefrontDao) {
		this.storefrontDao = storefrontDao;
	}

	public RestaurantDao getRestaurantDao() {
		return this.restaurantDao;
	}

	public void setRestaurantDao(RestaurantDao restaurantDao) {
		this.restaurantDao = restaurantDao;
	}

	@Override
	public List<StorefrontVO> getListData(final StorefrontVO sto) {
		return this.storefrontDao.finds(sto);
	}

	@Override
	public Map<String, Object> getListDataByPage(final StorefrontVO sto, final int pageNum, final int pageSize) {
		return this.storefrontDao.findByPage(sto, pageNum, pageSize);
	}

	@Override
	public int deleteData(final List<StorefrontVO> stoList) {
		/**
		 * 通过id或多个id查询出未完成的订单
		 */
		List<Map<String, Object>> list = this.storefrontDao.queryOrderByIds(stoList);
		/**
		 * 如果list的size大于0说明有的店面是有没有完成的订单，返回0，不允许此操作
		 */
		if (list.size() > 0) {
			return 0;
		}
		return this.storefrontDao.delete(stoList);
	}

	@Override
	public StorefrontVO queryById(String storefrontId) {
		return this.storefrontDao.queryById(storefrontId);
	}

	/**
	 *
	 * @Title: openSto
	 * @Description: 启用店面
	 * @author 孟凡岭
	 * @param @param idsArr
	 * @throws
	 */
	@Override
	public void openSto(String[] idsArr) {
		// TODO Auto-generated method stub
		/**
		 * 启用店面
		 */
		this.storefrontDao.openSto(idsArr);
		/**
		 * 启用餐厅，先查询出所属餐厅的ID
		 */
		List<Object> list = this.storefrontDao.queryRestaurantId(idsArr);
		List<Object> uniquerList = SetUniqueList.decorate(list);
		String ids = StringUtils.join(uniquerList.toArray(), ",");
		this.storefrontDao.updateParentRestaurant(ids, 1);
	}

	/**
	 *
	 * @Title: stopSto
	 * @Description: 停用店面
	 * @author 孟凡岭
	 * @param @param idsArr
	 * @param @return
	 * @throws
	 */
	@Override
	public boolean stopSto(String[] idsArr) {
		// TODO Auto-generated method stub
		/**
		 * 通过id或多个id查询出未完成的订单
		 */
		List<Map<String, Object>> list = this.storefrontDao.queryOrderByIds(idsArr);
		/**
		 * 如果list的size大于0说明有的店面是有没有完成的订单，返回false，不允许此操作
		 */
		if (list.size() > 0) {
			return false;
		}
		this.storefrontDao.stopSto(idsArr);
		/**
		 * 如果所属餐厅下已经没有店面，将餐厅停用
		 */
		List<Object> parent = this.storefrontDao.queryRestaurantId(idsArr);
		List<Object> uniquerList = SetUniqueList.decorate(parent);
		List<String> parentIds = this.storefrontDao.queryParentIds(uniquerList);
		this.storefrontDao.updateParentRestaurant(parentIds, 2);
		return true;
	}
}
